package com.fingard.dsp.bank.directbank.cmbunion02;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.cmbunion02.utils.SignatureUtil;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;

import java.util.HashMap;
import java.util.Map;

public class CMB02UnifiedOrder extends CMBUnion02Base {
    public RetAtsQueryOrder queryUnifiedOrder(ReqQueryOrder p_req) {
        RetAtsQueryOrder retAts = new RetAtsQueryOrder();
        retAts.rdSeq = p_req.rdSeq;
        try{
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "");
            if (actItem != null) {
                Map<String, String> requestPublicParams = getCommonMap();
                Map<String, Object> order = new HashMap<String, Object>();
                order.put("merId", actItem.merchantId);
                order.put("orderId", p_req.rdSeq);
                order.put("userId", actItem.userCode);
                WriteBankUrlLogLn("招商聚合扫码下单地址："+bankFront.serverURL+"/orderquery");
                ObjectMapper mapper = new ObjectMapper();
                requestPublicParams.put("biz_content", mapper.writeValueAsString(order));
                WriteBankLogLn("招商聚合加签前的报文内容：" + mapper.writeValueAsString(requestPublicParams));
                String signContent= SignatureUtil.getSignContent(requestPublicParams);
                String sign = SignatureUtil.rsaSign(signContent, actItem.ownKeyStorePath, "UTF-8");
                requestPublicParams.put("sign", sign);
                String[] tmpStrRet = sendToBank(sign,mapper.writeValueAsString(requestPublicParams),bankFront.serverURL+"/orderquery",actItem);
                if(tmpStrRet[0].length() == 0){
                    JSONObject resultObj = JSONObject.parseObject(tmpStrRet[1]);
                    if(resultObj.get("returnCode")!=null && "SUCCESS".equals(resultObj.get("returnCode"))){
                        String  respCode = resultObj.get("respCode").toString();
                        JSONObject biz_content = JSONObject.parseObject(resultObj.getString("biz_content"));
                        if(respCode!=null && "SUCCESS".equals(respCode)){
                            String tradeState = biz_content.get("tradeState").toString();
                            if("F".equals(tradeState)){
                                retAts.transState = respCode + "+" + tradeState;
                                retAts.payInfoCode = "交易失败";
                                retAts.respInfo = "交易失败";
                            } else {
                                retAts.transState = respCode + "+" + tradeState;
                                retAts.payInfoCode = respCode + "+" + tradeState;
                                retAts.respInfo = "请求成功";
                            }
                            retAts.respCode = "0";
                            retAts.outTransID = biz_content.getString("cmbOrderId");
                        }else {
                            retAts.respCode = "-1";
                            retAts.respInfo = resultObj.getString("respMsg");
                            String errCode = resultObj.getString("errCode");
                            if("ORDERID_NOT_EXIST".equals(errCode)){
                                retAts.respCode = "0";
                                retAts.respInfo = "订单不存在";
                                retAts.transState =respCode + "+" +errCode;
                            }
                        }
                    }else {
                        retAts.respCode = "-1";
                        retAts.respInfo = resultObj.getString("respMsg");
                    }
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        return retAts;
    }
}
